//
//  MainVC.m
//  iphone golf range assistant
//
//  Created by S W Consultancy on 05/09/2011.
//  Copyright 2011 SW Consultancy. All rights reserved.
//

#import "MainVC.h"
#import "RwData.h"
#import "InfoVC.h"

#include <stdlib.h>


@implementation MainVC

static int rand_range(long min, long max){// a random number in range
    max++;
    long result= random()%max;
    while(min>result){
        result = random()%max;
    }
    printf("min=%ld max=%ld result=%ld\n", min, max-1, result);
    return result;
}

static NSString* rand_str(long len, ...){// a random string, length then some NSString*'s
    NSString* this;
    long selected = rand_range(0, len);
    va_list ap;
    va_start(ap, len);
    for (long i=0; i<len; i++){
        this = va_arg(ap, NSString*);
        if (i==selected)break;
    }
    va_end(ap);
    return this;
}

static void btn_txt(UIButton* b, NSString* s){// put text on a button and make it visible
    [b setTitle:s forState:UIControlStateNormal];
    b.hidden = NO;
}

-(void)tally{
    hitsLabel.text = [NSString stringWithFormat:@"Sets: %d Balls:%d", sets, balls];
}

-(void)infoButtonClicked{
    NSLog(@"Info NYI");
    InfoVC *controller = [[InfoVC alloc] initWithNibName:nil bundle:nil];
    controller.delegate = self;
    
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    
}

-(void)newButtonClicked{
    [rwClubs empty];
    [rwRates empty];
    sets = 0;
    balls = 0;
    for (UIButton* c in clubs) {
        c.hidden = YES;
    }
    for (UIButton* b in rates) {
        b.hidden = YES;
    }
    btn_txt(nextButton, @"Start");
    hitsLabel.text = @"";
    newButton.hidden = YES;
}

-(void)nextButtonClicked{
    long numhits = rand_range(1, 5);
    //[self tally];
    for (UIButton* c in clubs) {
        c.hidden = YES;
    }

    if (numhits==1) {
        btn_txt(club1, rand_str(5, @"T3W", @"T5i", @"T6i", @"T7i", @"T3W"));
    }
    else if (numhits==2) {
        btn_txt(club1, rand_str(2, @"T8i", @"T7i"));
        btn_txt(club2, @"PW");
    } 
    else if (numhits==3) { 
        btn_txt(club1, rand_str(4, @"T3W", @"T7i", @"T6i", @"T3W"));
        btn_txt(club2, rand_str(2, @"7i", @"8i"));
        btn_txt(club3, rand_str(3, @"9i", @"SW", @"PW"));
    }
    else if (numhits==4) { 
        btn_txt(club1, rand_str(2, @"T3W", @"T7i"));
        btn_txt(club2, @"7i");
        btn_txt(club3, @"7i");
        btn_txt(club4, rand_str(3, @"9i", @"SW", @"PW"));
    }
    else if ((numhits==5)&&(rand_range(0,1))) { 
        btn_txt(club1, @"T3W");
        btn_txt(club2, @"7i");
        btn_txt(club3, @"7i");
        btn_txt(club4, rand_str(2, @"7i", @"8i"));
        btn_txt(club5, rand_str(4, @"8i", @"9i", @"SW", @"PW"));
    }
    else {
        NSString* which = rand_str(3, @"T3W", @"7i", @"T3W");
        for (UIButton* c in clubs) {
            btn_txt(c, which);
        }
    }
    btn_txt(nextButton, @"Next Set");
    nextButton.hidden = YES;
    newButton.hidden = YES;
}

-(void)clubClicked:(UIButton*)sender{
    NSLog(@"%@", sender.titleLabel.text);
    [rwClubs writeAsInt:sender.titleLabel.text :[rwClubs readAsInt:sender.titleLabel.text]+1];
    clubSelected = sender;
    [clubSelected setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    for (UIButton* b in rates) {
        b.hidden = NO;
    }
    balls ++;
    [self tally];
}

-(void)rateClicked:(UIButton*)sender{
    NSLog(@"%@", sender.titleLabel.text);
    [rwRates writeAsInt:sender.titleLabel.text :[rwRates readAsInt:sender.titleLabel.text]+1];
    clubSelected.hidden = YES;
    [clubSelected setTitleColor:[nextButton currentTitleColor] forState:UIControlStateNormal];
    for (UIButton* b in rates) {
        b.hidden = YES;
    }
    for (UIButton* b in clubs) {
        if (!b.hidden)
            return;
    }
    nextButton.hidden = NO;
    newButton.hidden = NO;
    sets++;
    [self tally];
}


#pragma mark - View lifecycle

-(UILabel*)addLabel :(NSString*)text :(int)w :(int)h :(float)x :(float)y{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.textAlignment = UITextAlignmentCenter;
    label.bounds = CGRectMake(0, 0, w, h);
    label.center = CGPointMake(x, y);
    label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;
    [mainView addSubview:label];
    return label;
}

-(UIButton*)addButton :(NSString*)text :(int)w :(int)h :(float)x :(float)y :(SEL)selector{ 
    UIButton*  button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.bounds = CGRectMake(0, 0, w, h);
    button.center = CGPointMake(x, y);
    button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin;
    [button setTitle:text forState:UIControlStateNormal];
    [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    button.hidden = YES;
    [mainView addSubview:button];
    return button;
}

- (void)loadView
{
    rwClubs = [RwData singleton:@"Clubs"];
    rwRates = [RwData singleton:@"Rates"];
    srandomdev(); //make it different every time!
    
    CGRect rect=[UIScreen mainScreen].applicationFrame;
    CGSize size=rect.size;

    mainView=[[UIView alloc] initWithFrame:rect];
    self.view = mainView;
    
    titleLabel = [self addLabel :@"Golf Range Assistant" :96*2 :32 :size.width/2 :1*size.height/8];
    hitsLabel = [self addLabel:@"" :size.width :32 :size.width/2 :2*size.height/8];

    club1 = [self addButton:@"" :size.width/5 :32 :1*size.width/10 :3*size.height/8 :@selector(clubClicked:)];
    club2 = [self addButton:@"" :size.width/5 :32 :3*size.width/10 :3*size.height/8 :@selector(clubClicked:)];
    club3 = [self addButton:@"" :size.width/5 :32 :5*size.width/10 :3*size.height/8 :@selector(clubClicked:)];
    club4 = [self addButton:@"" :size.width/5 :32 :7*size.width/10 :3*size.height/8 :@selector(clubClicked:)];
    club5 = [self addButton:@"" :size.width/5 :32 :9*size.width/10 :3*size.height/8 :@selector(clubClicked:)];
    clubs = [NSArray arrayWithObjects:club1, club2, club3, club4, club5, nil];

    good1  = [self addButton:@"Perfect"    :size.width/4 :32 :1*size.width/8 :4*size.height/8 :@selector(rateClicked:)];
    good2  = [self addButton:@"Good"       :size.width/4 :32 :1*size.width/8 :5*size.height/8 :@selector(rateClicked:)];
    good3  = [self addButton:@"OK"         :size.width/4 :32 :1*size.width/8 :6*size.height/8 :@selector(rateClicked:)];  
    top1   = [self addButton:@"Top"        :size.width/4 :32 :3*size.width/8 :4*size.height/8 :@selector(rateClicked:)];
    top2   = [self addButton:@"LongTop"    :size.width/4 :32 :3*size.width/8 :5*size.height/8 :@selector(rateClicked:)];
    top3   = [self addButton:@"ShortTop"   :size.width/4 :32 :3*size.width/8 :6*size.height/8 :@selector(rateClicked:)];  
    left1  = [self addButton:@"Left"       :size.width/4 :32 :5*size.width/8 :4*size.height/8 :@selector(rateClicked:)];
    left2  = [self addButton:@"LongLeft"   :size.width/4 :32 :5*size.width/8 :5*size.height/8 :@selector(rateClicked:)];
    left3  = [self addButton:@"ShortLeft"  :size.width/4 :32 :5*size.width/8 :6*size.height/8 :@selector(rateClicked:)];  
    right1 = [self addButton:@"Right"      :size.width/4 :32 :7*size.width/8 :4*size.height/8 :@selector(rateClicked:)];
    right2 = [self addButton:@"LongRight"  :size.width/4 :32 :7*size.width/8 :5*size.height/8 :@selector(rateClicked:)];
    right3 = [self addButton:@"ShortRight" :size.width/4 :32 :7*size.width/8 :6*size.height/8 :@selector(rateClicked:)];  
    rates = [NSArray arrayWithObjects:good1, good2, good3, top1, top2, top3, left1, left2, left3, right1, right2, right3, nil];

    newButton = [self addButton:@"New" :96 :32 :1*size.width/6 :7*size.height/8 :@selector(newButtonClicked)];
    nextButton = [self addButton:@"Start" :96 :32 :3*size.width/6 :3*size.height/8 :@selector(nextButtonClicked)];
    nextButton.hidden = NO;
    infoButton = [self addButton:@"Info" :96 :32 :5*size.width/6 :7*size.height/8 :@selector(infoButtonClicked)];
    infoButton.hidden = NO;
}

-(void)infoDone{
    [self dismissModalViewControllerAnimated:YES];
};

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    NSLog(@"rotate %d", interfaceOrientation);
    return YES; // any rotation allowed
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    NSLog(@"done rotate %d", fromInterfaceOrientation);
}

@end
